<?php
class Category_Form_QuickSearchRegularForm extends Ac_Form
{
    private $_preview_link = '';

    private $_category;
    private $_adid;

    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
        //array(array('row' => 'HtmlTag'), array('tag' => 'button'))

        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class'=>'button_em')),
        //array('Description', array('placement' => 'APPEND', 'tag'=>'', 'escape' => false)),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
        //'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array('Label', array('placement' => 'prepend', 'id' => 'agentname_label')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'p')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $formDecorators = array(
            //'FormElements',
            //array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            //array('Description', array('placement' => 'prepend', 'tag'=>'', 'escape' => false)),
            //'fieldset',
            //'Form',
            'PrepareElements',
            array('ViewScript', array('viewScript' => 'index/customization/regular.phtml'))
    );

    protected $subFormDecorators = array(
        'FormElements',
        //array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
        array('Description', array('placement' => 'prepend')),
        'fieldset',
    );

    protected $spanDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('placement' => 'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $displayGroupDecorators = array(
        'FormElements',
        //array(array('outerHTmlTag' => 'HtmlTag'), array('tag' => 'p', 'class' => 'price radio text')),
        array('HtmlTag', array('tag' => 'p', 'class' => 'price radio text')),
        array('Description', array('placement' => 'prepend', 'tag' => 'label')),
    );

    protected $elementCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array('description', array('placement' => 'APPEND', 'tag' => 'span')),
        array('label', array('placement' => 'APPEND', 'class' => '')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $elementMultiCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'span', 'class' => 'multiCheckbox')),
        array('Label', array('placement' => 'APPEND')),
    );

    protected $elementRadioDecorators = array(
        'ViewHelper',
        'Errors',
        // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'span', 'class' => 'multiCheckbox')),
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    public function __construct($options = array())
    {
        $this->_preview_link = !empty($options['preview_link']) ? $options['preview_link'] : '';
        $this->_category = !empty($options['category']) ? $options['category'] : array();
        $this->_adid = !empty($options['adid']) ? $options['adid'] : array();
        parent::__construct($options);
    }

    public function init()
    {
        parent::init();

        $this->setMethod('get');
        $this->setAction($this->getView()->geturl('regular' ,'search'));

        $element = new Zend_Form_Element_Hidden('adid');
        $element->setValue((string) $this->_adid);
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        $element = new Zend_Form_Element_Hidden('small_category_list');
        if (!empty($this->_category)) {
            $element->setValue((string) $this->_category->_id);
        } elseif (Zend_Registry::isRegistered('category_list_selected')) {
            $selected = Zend_Registry::get('category_list_selected');
            $element->setValue($selected);
        } elseif (!empty($_GET['small_category_list'])) {
            $selected = $_GET['small_category_list'];
            $element->setValue($selected);
        }
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);


        //Category
       

        // -> Price
        /*
        $this->addElement('jqslider', 'price', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 20000000,
                        'values'   => array('0', '20000000'),
                        'currency' => '$HKD',
                        'range'    => true,
                        'step'     => 1000,
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                    )
                )
        );
        */

        $this->addElement('textRange', 'price', array(
                    'value'       => array(),
                    'class'       => array('class' => 'text-range'),
                    'decorators'  => array(
                            'Errors',
                            'ViewHelper',
                    )
                )
        );

        //Has Image
        $this->addElement('checkbox', 'has_image', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'Label' => 'Has Image',
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementCheckBoxDecorators,
            'required'   => true,
        ));

        //Area
        /*
        $areas = Model_Location::getAreaSearch();        
        $selected_area = empty($_POST['area']) ? 0 : $_POST['area'];
        if (empty($selected_area)) {
            reset($areas);
            $selected_area = key($areas);
        }

        $this->addElement('select', 'area', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                'Alnum',
                array('StringLength', false, 1),
            ),
            'multiOptions' => $areas,
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => false,
        ));
         *
         */
        $locations = array('All');
        $locs = Model_Location::all()->sort(array('path' => 1));
        $parent = '';
        foreach($locs as $k => $loc) {

            if (count($loc->path) == 1) {
                $parent = $loc->name;
                $locations[$loc->name] = array();
            } else {

                //$parent = (string) $loc->path[0];
                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();

                $tmp[$k]         = (string) $loc->name;
                //echo '<pre>';
                //print_r($tmp);
                $locations[(string) $parent] = $tmp;
            }
        }
        
        $locations = array('All');
        $locs = Model_Location::all()->sort(array('path' => 1));
        $parent = '';
        foreach($locs as $k => $loc) {

            if (count($loc->path) == 1) {
                $parent = $loc->name;
                $locations[$loc->name] = array();
            } else {

                //$parent = (string) $loc->path[0];
                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();

                $tmp[$k]         = (string) $loc->name;
                //echo '<pre>';
                //print_r($tmp);
                $locations[(string) $parent] = $tmp;
            }
        }
        // --> District
        $locations = array('All');
        $locs = Model_Location::all()->sort(array('path' => 1));
        $parent = '';
        foreach($locs as $k => $loc) {

            if (count($loc->path) == 1) {
                $parent = $loc->name;
                $locations[$loc->name] = array();
            } else {

                //$parent = (string) $loc->path[0];
                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();

                $tmp[$k]         = (string) $loc->name;
                //echo '<pre>';
                //print_r($tmp);
                $locations[(string) $parent] = $tmp;
            }
        }
        $this->addElement('select', 'district', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'multiOptions'  => $locations,
            'required'      => false,
            'label'         => 'District',
        ));    

        $this->addElement('button', 'v', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Search',
            'decorators'  => $this->elementButtonDecorators,
        ));

    }

    public function __toString()
    {
        $str = '
            $j("#area").change(function (event) {
                event.stopImmediatePropagation();
                $j.get("/search/get-attribute", { c:"area", f:"district_bis", id: $j("#area").val() },
                    function(data) {
                        $j("#district").empty();
                        $j.each(data, function (index, value) {
                            $(document.createElement("option"))
                                .html(value)
                                .attr("value", index)
                                .appendTo($("#district"));
                        });
                    });
           });
        ';
        $this->view->JQuery()->addJavascript($str);
        return parent::__toString();
    }
}